package net.ssta.core.util.format;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;

public class ValueObjectFormat
{
	private static Map<String,ValueObjectFormat> s_class_visitor_Map = new HashMap<String,ValueObjectFormat>();
	public static ValueObjectFormat getFormat(String objectClassName)
	{
		ValueObjectFormat formatter = s_class_visitor_Map.get(objectClassName == null ? "null" : objectClassName);
		if(formatter == null)
		{
			throw new IllegalArgumentException("this data type is not supported! your type is " + objectClassName);
		}
		return formatter;
	}
	
	public static ValueObjectFormat getFormat(Object obj)
	{
		String objectClassName = ( obj == null ) ? "null" : obj.getClass().getName();
		ValueObjectFormat formatter = s_class_visitor_Map.get(objectClassName == null ? "null" : objectClassName);
		if(formatter == null)
		{
			throw new IllegalArgumentException("this data type is not supported! your type is " + objectClassName);
		}
		return formatter;
	}
	
	public static void setColumnVisitor(String className, ValueObjectFormat formatter)
	{
		s_class_visitor_Map.put(className, formatter);
	}
	
	protected ValueObjectFormat(){}
	
	public BigDecimal getBigDecimal(Object value){ return BigDecimal.ZERO; }
	
	public short getShort(Object value){return 0;}
	
	public int getInt(Object value){ return 0; }

	public float getFloat(Object value){ return 0; }

	public long getLong(Object value){ return 0; }

	public double getDouble(Object value){ return 0; }
	
	public String getString(Object value, String defaultValue) { return value == null ? defaultValue : value.toString(); }
	
	public String getString(Object value, String defaultValue, String formatString) { return value == null ? defaultValue : value.toString(); }

	public String[] getStringArray(Object value)
	{
		throw new RuntimeException("this data type '" + value.getClass().getName() + "' can't convert to an array.");
	}
	
	public byte[] getBytes(Object value){ return new byte[0]; }
	
	public InputStream getInputStream(Object value){ return null;}
	
	public Reader getReader(Object value){ return null;}
	
	public int getLength(Object value){return 0;}
	
	protected String getString(Reader reader) throws IOException
	{
		BufferedReader br = new BufferedReader(reader);
		StringBuffer sb = new StringBuffer();
		String line = br.readLine();
		while( line !=null )
		{
			sb.append(line);
			line=br.readLine();
			if(line != null)
			{
				sb.append("\r\n");
			}
		}
		return sb.toString();
	}
	
	public Boolean getJavaBoolean(Object value)	{ return Boolean.FALSE; }
	
	public java.util.Date getJavaUtilDate(Object value, String formatString)	{ return s_class_visitor_Map.get("java.util.Date").getJavaUtilDate(value, "yyyy-mm-dd"); }
	public java.sql.Date getJavaSqlDate(Object value, String formatString)	{ return null; }
	public Timestamp getJavaSqlTimestamp(Object value, String formatString)	{ return null; }
	
	static
	{
		//JavaIOReaderFormat/JavaTimestampFormat
		s_class_visitor_Map.put("null", new ValueObjectFormat());
		s_class_visitor_Map.put("[B", new JavaByteArrayFormat());
		s_class_visitor_Map.put("java.lang.String", new JavaStringFormat());
		s_class_visitor_Map.put("[Ljava.lang.String;", new StringArrayFormat());
		s_class_visitor_Map.put("java.lang.Integer", new JavaIntegerFormat());
		s_class_visitor_Map.put("java.lang.Long", new JavaLongFormat());
		s_class_visitor_Map.put("java.lang.Float", new JavaFloatFormat());
		s_class_visitor_Map.put("java.lang.Double", new JavaDoubleFormat());
		s_class_visitor_Map.put("java.math.BigDecimal", new JavaBigDecimalFormat());
		s_class_visitor_Map.put("java.lang.Boolean", new JavaBooleanFormat());
		s_class_visitor_Map.put("java.util.Date", new JavaUtilDateFormat());
		s_class_visitor_Map.put("java.sql.Date", new JavaTimestampFormat());
		s_class_visitor_Map.put("java.sql.Timestamp", new JavaTimestampFormat());
		s_class_visitor_Map.put("java.io.InputStream", new JavaInputStreamFormat());
		s_class_visitor_Map.put("java.io.FileInputStream", new JavaInputStreamFormat());
		s_class_visitor_Map.put("oracle.sql.CLOB", new OracleClobFormat());
		s_class_visitor_Map.put("oracle.sql.BLOB", new OracleBlobFormat());
		s_class_visitor_Map.put("oracle.sql.TIMESTAMP", new OracleTimestampFormat());
//		s_class_visitor_Map.put("oracle.sql.TIMESTAMPTZ", new OracleTimestampColumnVisitor());
//		s_class_visitor_Map.put("oracle.sql.TIMESTAMPLTZ", new OracleTimestampColumnVisitor());
	}	
}
